package com.powernode.crm.settings.web;

import com.powernode.crm.commons.model.Constants;
import com.powernode.crm.commons.model.ResponseResult;
import com.powernode.crm.commons.utils.DateUtils;
import com.powernode.crm.settings.model.User;
import com.powernode.crm.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/settings")
public class SettingController {

    @Autowired
    private UserService userService;

    @RequestMapping("/user/login")
    public String login(){
        return "settings/qx/user/login";
    }

    /*登录*/
    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult doLogin(
            String loginAct,
            String loginPwd,
            HttpServletRequest request,
            HttpSession session,
            boolean rememberMe,
            HttpServletResponse response
    ){

        ResponseResult result = new ResponseResult();
        /*登录验证-账号密码*/
        User loginUser = userService.queryUserByLoginActAndLoginPwd(loginAct, loginPwd);
        if (loginUser==null){
            result.setCode(Constants.STATUS_LOGINERROR);
            result.setMsg("账号密码错误");
            return result;
        }
        /*状态*/
        if ("0".equals(loginUser.getLockState())){//“0”.方法 防止空指针异常
            result.setCode(Constants.STATUS_LOCKED);
            result.setMsg("账号被锁定");
            return result;
        }
        /*过期时间*/
        String now = DateUtils.formatDateTime(new Date());//当前时间的字符串
        if (now.compareTo(loginUser.getExpireTime())>0){//  >0 左边比右边大   <0 右边比左边小   ==0 相等
            result.setCode(Constants.STATUS_EXPIRED);
            result.setMsg("账号已过期");
            return result;
        }
        /*ip*/
//        获取当前用户 使用ip
        String ip = request.getRemoteAddr();
        /*
        * 允许访问的IP为空时表示IP地址永不受限，
        * 允许访问的IP可以是一个，也可以是多个，
        * 当多个IP地址的时候，采用半角逗号分隔。
        * 允许IP是192.168.100.2，表示该用户只能在IP地址为192.168.100.2的机器上使用。
        * */
        if (loginUser.getAllowIps()!=null&&!loginUser.getAllowIps().equals("")&&!loginUser.getAllowIps().contains(ip)){
            result.setCode(Constants.STATUS_IP);
            result.setMsg("IP受限");
            return result;
        }
        /*session区间 储存  user*/
        session.setAttribute(Constants.SESSION_USER,loginUser);
        result.setCode(Constants.STATUS_OK);
        result.setMsg("登录成功");

//        10天免登录
        //            创建cookie对象
        Cookie loginActCookie = new Cookie("loginAct",loginAct);
        Cookie loginPwdCookie = new Cookie("loginPwd",loginPwd);
//        通过判断前端是否勾选
        if (rememberMe){
//            设置cookie 时效
            loginActCookie.setMaxAge(10*24*60*60);
//            cookie 需要 写入 到客户端
            loginPwdCookie.setMaxAge(10*24*60*60);
        }else {
//            清楚原有的 cookie
            loginActCookie.setMaxAge(0);
            loginPwdCookie.setMaxAge(0);
        }
        response.addCookie(loginActCookie);
        response.addCookie(loginPwdCookie);
        return result;
    }

    /*退出*/
    @RequestMapping("/logout")
    public String logout(HttpSession session,HttpServletResponse response){
        session.invalidate();
        Cookie loginActCookie = new Cookie("loginAct","");
        Cookie loginPwdCookie = new Cookie("loginPwd","");
        loginActCookie.setMaxAge(0);
        loginPwdCookie.setMaxAge(0);
        response.addCookie(loginActCookie);
        response.addCookie(loginPwdCookie);
        return "redirect:/settings/user/login";
    }

}
